package cn.csg.iotplatform.module.system.controller.admin.userzone;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.csg.iotplatform.framework.common.pojo.PageParam;
import cn.csg.iotplatform.framework.common.pojo.PageResult;
import cn.csg.iotplatform.framework.common.pojo.CommonResult;
import cn.csg.iotplatform.framework.common.util.object.BeanUtils;
import static cn.csg.iotplatform.framework.common.pojo.CommonResult.success;

import cn.csg.iotplatform.framework.excel.core.util.ExcelUtils;

import cn.csg.iotplatform.framework.operatelog.core.annotations.OperateLog;
import static cn.csg.iotplatform.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csg.iotplatform.module.system.controller.admin.userzone.vo.*;
import cn.csg.iotplatform.module.system.dal.dataobject.userzone.UserZoneDO;
import cn.csg.iotplatform.module.system.service.userzone.UserZoneService;

@Tag(name = "管理后台 - 用户分区关系")
@RestController
@RequestMapping("/system/user-zone")
@Validated
public class UserZoneController {

    @Resource
    private UserZoneService userZoneService;

    @PostMapping("/create")
    @Operation(summary = "创建用户分区关系")
    @PreAuthorize("@ss.hasPermission('system:user-zone:create')")
    public CommonResult<Long> createUserZone(@Valid @RequestBody UserZoneSaveReqVO createReqVO) {
        return success(userZoneService.createUserZone(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户分区关系")
    @PreAuthorize("@ss.hasPermission('system:user-zone:update')")
    public CommonResult<Boolean> updateUserZone(@Valid @RequestBody UserZoneSaveReqVO updateReqVO) {
        userZoneService.updateUserZone(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户分区关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-zone:delete')")
    public CommonResult<Boolean> deleteUserZone(@RequestParam("id") Long id) {
        userZoneService.deleteUserZone(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户分区关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-zone:query')")
    public CommonResult<UserZoneRespVO> getUserZone(@RequestParam("id") Long id) {
        UserZoneDO userZone = userZoneService.getUserZone(id);
        return success(BeanUtils.toBean(userZone, UserZoneRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户分区关系分页")
    @PreAuthorize("@ss.hasPermission('system:user-zone:query')")
    public CommonResult<PageResult<UserZoneRespVO>> getUserZonePage(@Valid UserZonePageReqVO pageReqVO) {
        PageResult<UserZoneDO> pageResult = userZoneService.getUserZonePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserZoneRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户分区关系 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-zone:export')")
    @OperateLog(type = EXPORT)
    public void exportUserZoneExcel(@Valid UserZonePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserZoneDO> list = userZoneService.getUserZonePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户分区关系.xls", "数据", UserZoneRespVO.class,
                        BeanUtils.toBean(list, UserZoneRespVO.class));
    }

}